package com.ls.sort_algorithm;

import java.util.Arrays;

//归并排序
public class MergerSort {
    public static void main(String[] args) {
        MergerSort mergeSort = new MergerSort();
        int[] arr = {3, 7, 9, -1, 16, 1, -2, 10};
//        int[] arr = {-9,78,0,23,-10,70};
        int[] temp = new int[arr.length];
        mergeSort.mergeSort2(arr, 0, arr.length - 1, temp);
        System.out.println(Arrays.toString(arr));

    }


    public void mergeSort(int[] arr, int left, int right, int[] temp) {

        if (left < right) {//最终找到两个数,只有一个数就不进了
            System.out.println(left + "--" + right);
            int mid = (left + right) / 2;
            //向左递归进行分解
            mergeSort(arr, left, mid, temp);
            //向右递归进行分解
            mergeSort(arr, mid + 1, right, temp);
            //合并
            merge(arr, left, mid, right, temp);
        }
    }

    /**
     * 合并的方法
     *
     * @param arr
     * @param left  左边有序序列的初始索引
     * @param mid   中间索引
     * @param right 右侧边界索引
     * @param temp  中转数组
     */
    public void merge(int[] arr, int left, int mid, int right, int[] temp) {
        int i = left;//初始化i,左边有序序列的初始索引
        int j = mid + 1;//初始化j,右边有序序列的初始索引
        int t = 0;//指向temp数组的当前索引
//        System.out.println(left+"--"+right);

        //(一)
        //先把左右两边(有序)的数按照规则填充到temp数组
        //知道左右两边的有序序列,有一边处理完毕为止
        while (i <= mid && j <= right) {
            if (arr[i] <= arr[j]) {
                temp[t] = arr[i];
                t++;
                i++;
            } else {
                temp[t] = arr[j];
                t++;
                j++;
            }
        }
        //(二)
        //把有剩余数据的一边的数据依次全部填充到temp
        while (i <= mid) {//左边有剩余元素,就全部填充到temp中
            temp[t] = arr[i];
            t++;
            i++;
        }
        while (j <= right) {//右边有剩余元素,就全部填充到temp中
            temp[t] = arr[j];
            t++;
            j++;
        }
        //(三)
        //将temp中元素拷贝到arr
        t = 0;
        int tempLeft = left;
        while (tempLeft <= right) {//第一次合并时tempLeft = 0, right = 1 | tempLeft = 2, right = 3...
            arr[tempLeft] = temp[t];
            t++;
            tempLeft++;
        }
//        System.out.println(Arrays.toString(temp));
    }


    public void mergeSort2(int[] arr, int left, int right, int[] temp) {
        if (left < right) {
            int mid = (left + right) / 2;
            mergeSort2(arr, left, mid, temp);
            mergeSort2(arr, mid + 1, right, temp);
            merge2(arr, left, mid, right, temp);
        }
    }

    public void merge2(int[] arr, int left, int mid, int right, int[] temp) {
        int l = left;
        int r = mid+1;
        int t = 0;
        while (l <= mid && r <= right) {
            if (arr[l] < arr[r]) {
                temp[t] = arr[l];
                l++;
                t++;
            } else {
                temp[t] = arr[r];
                r++;
                t++;
            }
        }

        while (l <= mid) {
            temp[t] = arr[l];
            l++;
            t++;
        }
        while (r <= right) {
            temp[t] = arr[r];
            r++;
            t++;
        }
        t = 0;
        int tempLeft = left;
        while (tempLeft <= right) {
            arr[tempLeft] = temp[t];
            tempLeft++;
            t++;
        }
    }
}
